const express = require('express');
const router = express.Router();
const StudentActivity = require('../../models/SjoinActivity/SjoinActivity');

// 获取活动的已报名学生信息
router.get('/activityIdregistered/:activityId', async (req, res) => {
  try {
    const { activityId } = req.params;
    const { page = 1, pageSize = 10 } = req.query;
    const offset = (page - 1) * pageSize;

    // 获取已报名学生记录
    const { count, rows } = await StudentActivity.findAndCountAll({
      where: {
        activity_id: activityId,
        status: 'registered'
      },
      limit: parseInt(pageSize),
      offset: parseInt(offset),
      order: [['register_time', 'DESC']]
    });

    res.json({
      success: true,
      data: {
        total: count,
        page: parseInt(page),
        pageSize: parseInt(pageSize),
        records: rows
      }
    });
  } catch (error) {
    console.error('获取活动报名记录失败:', error);
    res.status(500).json({
      success: false,
      message: '获取活动报名记录失败',
      error: error.message
    });
  }
});

// 获取学生已报名的活动信息
router.get('/studentRegistered/:studentId', async (req, res) => {
  try {
    const { studentId } = req.params;
    const { page = 1, pageSize = 10 } = req.query;
    const offset = (page - 1) * pageSize;

    // 获取学生已报名的活动记录
    const { count, rows } = await StudentActivity.findAndCountAll({
      where: {
        student_id: studentId,
        status: 'registered'
      },
      limit: parseInt(pageSize),
      offset: parseInt(offset),
      order: [['register_time', 'DESC']]
    });

    res.json({
      success: true,
      data: {
        total: count,
        page: parseInt(page),
        pageSize: parseInt(pageSize),
        records: rows
      }
    });
  } catch (error) {
    console.error('获取学生报名记录失败:', error);
    res.status(500).json({
      success: false,
      message: '获取学生报名记录失败',
      error: error.message
    });
  }
});

// 获取学生已参加的活动信息
router.get('/studentAttended/:studentId', async (req, res) => {
  try {
    const { studentId } = req.params;
    const { page = 1, pageSize = 10 } = req.query;
    const offset = (page - 1) * pageSize;

    // 获取学生已参加的活动记录
    const { count, rows } = await StudentActivity.findAndCountAll({
      where: {
        student_id: studentId,
        status: 'attended'
      },
      limit: parseInt(pageSize),
      offset: parseInt(offset),
      order: [['attend_time', 'DESC']]
    });

    // 计算总积分
    const totalPoints = rows.reduce((sum, record) => sum + (parseFloat(record.points_awarded) || 0), 0);

    res.json({
      success: true,
      data: {
        total: count,
        page: parseInt(page),
        pageSize: parseInt(pageSize),
        totalPoints: totalPoints.toFixed(2),
        records: rows
      }
    });
  } catch (error) {
    console.error('获取学生已参加活动记录失败:', error);
    res.status(500).json({
      success: false,
      message: '获取学生已参加活动记录失败',
      error: error.message
    });
  }
});

// 根据学生ID和活动ID获取学生活动信息
router.get('/studentActivity/:studentId/:activityId', async (req, res) => {
  try {
    const { studentId, activityId } = req.params;

    // 查找学生活动记录
    const studentActivity = await StudentActivity.findOne({
      where: {
        student_id: studentId,
        activity_id: activityId
      }
    });

    if (!studentActivity) {
      return res.status(404).json({
        success: false,
        message: '未找到该学生和活动的匹配记录'
      });
    }

    res.json({
      success: true,
      data: studentActivity
    });

  } catch (error) {
    console.error('根据学生ID和活动ID获取学生活动信息失败:', error);
    res.status(500).json({
      success: false,
      message: '获取学生活动信息失败',
      error: error.message
    });
  }
});

// 根据学生ID和活动ID获取未参加的活动信息
router.get('/studentMissed/:studentId/:activityId', async (req, res) => {
  try {
    const { studentId, activityId } = req.params;

    // 查找学生未参加的活动记录
    const studentActivity = await StudentActivity.findOne({
      where: {
        student_id: studentId,
        activity_id: activityId,
        status: 'missed'
      }
    });

    if (!studentActivity) {
      return res.status(404).json({
        success: false,
        message: '未找到该学生未参加的活动记录'
      });
    }

    res.json({
      success: true,
      data: studentActivity
    });

  } catch (error) {
    console.error('获取学生未参加活动记录失败:', error);
    res.status(500).json({
      success: false,
      message: '获取学生未参加活动记录失败',
      error: error.message
    });
  }
});

// 获取学生所有未参加的活动信息
router.get('/studentAllMissed/:studentId', async (req, res) => {
  try {
    const { studentId } = req.params;
    const { page = 1, pageSize = 10 } = req.query;
    const offset = (page - 1) * pageSize;

    // 获取学生所有未参加的活动记录
    const { count, rows } = await StudentActivity.findAndCountAll({
      where: {
        student_id: studentId,
        status: 'missed'
      },
      limit: parseInt(pageSize),
      offset: parseInt(offset),
      order: [['register_time', 'DESC']]
    });

    res.json({
      success: true,
      data: {
        total: count,
        page: parseInt(page),
        pageSize: parseInt(pageSize),
        records: rows
      }
    });
  } catch (error) {
    console.error('获取学生未参加活动记录失败:', error);
    res.status(500).json({
      success: false,
      message: '获取学生未参加活动记录失败',
      error: error.message
    });
  }
});

module.exports = router;
